package com.lbssoft.gpsbd.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lbssoft.datalink.EntityManager;
import com.lbssoft.datalink.util.Log;
import com.lbssoft.gpsbd.common.Constant;
import com.lbssoft.gpsbd.dao.CommandMapper;
import com.lbssoft.gpsbd.dao.CommandRecordMapper;
import com.lbssoft.gpsbd.dao.DeviceMapper;
import com.lbssoft.gpsbd.datalink.Context;
import com.lbssoft.gpsbd.model.Command;
import com.lbssoft.gpsbd.model.CommandRecord;
import com.lbssoft.gpsbd.model.Device;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.service.CommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by nczc on 2018/6/28.
 */
@Service
public class CommandServiceImpl implements CommandService {

    @Autowired
    CommandMapper commandMapper;

    @Autowired
    DeviceMapper deviceMapper;

    @Autowired
    CommandRecordMapper commandRecordMapper;


    @Override
    public Result getViews() {
        Result result = new Result();
        Map data = new HashMap();
        data.put("views", commandMapper.views());
        data.put("commandIdsByType", commandMapper.getCommandIdsByType());
        result.setCode(0);
        result.setData(data);
        return result;
    }


    @Override
    public Result send(Map command) {
        Result result = new Result();
        if(!command.containsKey("deviceId")){
            result.setCode(1);
            result.setMsg("请输入设备id");
            return result;
        }
        if(!command.containsKey("id")){
            result.setCode(1);
            result.setMsg("请输入命令id");
            return result;
        }

        String id = command.get("id").toString();
        long deviceId = Integer.parseInt(command.get("deviceId").toString());
        Command cmd = commandMapper.getCommand(Integer.parseInt(id));
        JSONArray views = JSON.parseArray(cmd.getView());
        Map attributes = new HashMap();
        for(int i=0;i<views.size();i++){
            String name = JSON.parseObject(views.get(i).toString()).getString("name");
            String type = JSON.parseObject(views.get(i).toString()).getString("type");
            if(!type.equals("button")&&!command.containsKey(name)){
                result.setCode(1);
                result.setMsg("缺少参数："+name);
                return result;

            }
            if(cmd.getType().equals("OnOff")){//开关类型 把值作为类型传过去即可
                cmd.setType(command.get(name).toString());
            }

            attributes.put(name, command.get(name));
        }
        //流水号
        int sn=0;
        if(!cmd.getType().equals("custom")){
            sn = commandRecordMapper.getNewSn(deviceId);
        }

        attributes.put("sn", sn);


        CommandRecord record = new CommandRecord();
        Device device = deviceMapper.getDeviceById(deviceId);
        com.lbssoft.datalink.model.Result res = Context.entityManager.sendCommand(device.getUuid(),cmd.getType(),attributes);
        commandRecordMapper.updateUnexecuted(deviceId);

        record.setCmdId(Integer.parseInt(id));
        record.setSn(sn);
        record.setDeviceId(Long.parseLong(command.get("deviceId").toString()));
        record.setReq(JSON.toJSONString(attributes));

        if(res.getCode()==0){
            result.setCode(0);
            record.setStatus(2);
        }else{
            result.setCode(1);
            result.setMsg("CommandCached");
            record.setStatus(1);
        }
        commandRecordMapper.add(record);
        return result;
    }
}
